
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.view.*;
import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;

/**
 * 授权api
 */
 
@Api(tags = "Authorize",description = "授权")
@RestController
public class AuthorizeController {
    
    @Autowired
    private AuthorizeDao dao;
   
    /**
     * 获取授权集合
     * @return api结果
     */
    @ApiOperation(value = "获取授权集合")
    @RequestMapping(value =  "/api/Authorize/",method = {RequestMethod.GET})
    public ApiResult<List<Authorize>> Get(){
         ApiResult<List<Authorize>> result=new ApiResult<List<Authorize>>();
        try 
        {
            List<Authorize> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 获取权限树型结构
     * @return api结果
     */
    @ApiOperation(value = "获取权限树型结构")
    @RequestMapping(value =  "/api/Authorize/Tree",method = {RequestMethod.GET})
    public ApiResult<List<AuthorizeTree>> Tree(){
        ApiResult<List<AuthorizeTree>> result=new ApiResult<List<AuthorizeTree>>();
        try
        {
            List<AuthorizeTree> list=new ArrayList<AuthorizeTree>();
            List<Authorize> list2=dao.findAll();

            for (Authorize authorize : list2) {
                if  (authorize.getLevel()==authorize.getParentId() && Long.parseLong("0")==authorize.getLevel()){
                    AuthorizeTree tree = new AuthorizeTree(authorize);

                    tree.addChildren(list2);

                    list.add(tree);
                }
            }

            result.setData(list);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据项目角色ID获取权限树
     * @param projectRoleId 项目角色ID
     * @return api结果
     */
    @ApiOperation(value = "根据项目角色ID获取权限树")
    @RequestMapping(value =  "/api/Authorize/ProjectRole/{projectRoleId}",method = {RequestMethod.GET})
    public ApiResult<List<AuthorizeTree>> Project(@PathVariable Long projectRoleId){
        ApiResult<List<AuthorizeTree>> result=new ApiResult<List<AuthorizeTree>>();
        try
        {
            List<Authorize> model= dao.findByProjectRoleId(projectRoleId);
            List<AuthorizeTree> list=new ArrayList<AuthorizeTree>();

            for (Authorize authorize : model) {
                if  (authorize.getLevel()==authorize.getParentId() && Long.parseLong("0")==authorize.getLevel()){
                    AuthorizeTree tree = new AuthorizeTree(authorize);

                    tree.addChildren(model);
                    list.add(tree);
                }
            }

            result.setData(list);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取授权信息
     * @param id 授权ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取授权信息")
    @RequestMapping(value =  "/api/Authorize/{id}",method = {RequestMethod.GET})
    public ApiResult<Authorize> Get(@PathVariable Long id){
        ApiResult<Authorize> result=new ApiResult<Authorize>();
        try
        {
            Authorize model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改授权信息
     * @param id 授权ID
     * @param model 授权信息
     * @return api结果
     */
    @ApiOperation(value = "修改授权信息")
    @RequestMapping(value =  "/api/Authorize/{id}",method = {RequestMethod.PUT})
    public ApiResult<Authorize> Edit(@PathVariable Long id,@RequestBody Authorize model){
        ApiResult<Authorize> result=new ApiResult<Authorize>();
        try
        {
            model.setAuthorizeId(id);
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加授权信息
     * @param model 授权信息
     * @return api结果
     */
    @ApiOperation(value = "添加授权信息")
    @RequestMapping(value =  "/api/Authorize/",method = {RequestMethod.POST})
    public ApiResult<Authorize> Add(@RequestBody Authorize model){
         ApiResult<Authorize> result=new ApiResult<Authorize>();
        try
        {
            model.setCreateDate(new Date());
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除授权信息
     * @param id 授权ID
     * @return api结果
     */
    @ApiOperation(value = "删除授权信息")
    @RequestMapping(value =  "/api/Authorize/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

